package cn.wjdiankong.levideo.view;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.Transformation;
import cn.wjdiankong.levideo.R;

public class LevideoDialog extends Dialog{
	
    private View mDialogView;
    private AnimationSet mModalInAnim;
    private AnimationSet mModalOutAnim;
    private Animation mOverlayOutAnim;
    
    public static final int SIZE_LARGE = 1;

    public static final int SIZE_SMALL = 2;

    public LevideoDialog(Context context, View view, int width, int height) {
    	super(context, R.style.alert_dialog);
    	setContentView(view);
    	mDialogView = view;
    	
    	LayoutParams p = getWindow().getAttributes(); 
    	if(width > 0){
    		p.width = width;
    	}
    	if(height > 0){
    		p.height = height;
    	}
    	getWindow().setAttributes(p);
    	
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        
        mModalInAnim = (AnimationSet) OptAnimationLoader.loadAnimation(getContext(), R.anim.modal_in);
        mModalOutAnim = (AnimationSet) OptAnimationLoader.loadAnimation(getContext(), R.anim.modal_out);
        mModalOutAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mDialogView.post(new Runnable() {
                    @Override
                    public void run() {
                    	LevideoDialog.super.dismiss();
                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        mOverlayOutAnim = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                LayoutParams wlp = getWindow().getAttributes();
                wlp.alpha = 1 - interpolatedTime;
                getWindow().setAttributes(wlp);
            }
        };
        mOverlayOutAnim.setDuration(120);
    }
    
    public View getContentView(){
    	return mDialogView;
    }

    protected void onStart() {
        mDialogView.startAnimation(mModalInAnim);
    }

    @Override
    public void cancel() {
        dismissWithAnimation(true);
    }

    public void dismissWithAnimation() {
        dismissWithAnimation(false);
    }

    private void dismissWithAnimation(boolean fromCancel) {
        mDialogView.startAnimation(mModalOutAnim);
    }
    
}